package com.example.monthmoni.controller;

import jakarta.annotation.Resource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @Author 宋伟宁
 * @Date 2023/11/28
 * @Version 1.0
 **/
@Controller
public class IndexController {

    @Resource
    private RedisTemplate redisTemplate;
    @GetMapping("/login")
    public String tologin(Model model){
        //产生一个随机数，并存放到redis中，有效时间为一分钟
        String codes="0123456789";
        Random random = new Random();
        StringBuilder sb= new StringBuilder();
        for(int i=0;i<4;i++){
              sb.append( codes.charAt(random.nextInt(codes.length())));
        }
        String code= sb.toString();
        model.addAttribute("code",code);
        //存放到redis
        this.redisTemplate.opsForValue().set("mycode",code,1, TimeUnit.MINUTES);
        return "login";
    }

    @GetMapping("/index")
    public String index(){
        return "index";
    }
}
